fix(db): StaticPool for in-memory SQLite + module-cache reset between tests#223
Merged
Conversation
…tween tests Architectural fix for the in-memory SQLite + aiosqlite combination used by the test suite. Problem: every async_session() call may receive a separate connection from the default pool. With sqlite+aiosqlite:// (no path), every fresh connection opens its OWN in-memory database, so a row written by session A is invisible to session B. This causes intermittent test failures and made earlier conftest cleanup-fixture attempts strictly worse (a row-wipe ran on a different in-memory DB than the test code read). Fix: src/sandcastle/models/db.py now detects in-memory SQLite URLs (via the new _is_in_memory_sqlite helper) and forces poolclass=StaticPool. All sessions then share one connection and one database for the lifetime of the process. No behavior change for production - file-backed SQLite and PostgreSQL paths are untouched. Plus a function-scoped autouse conftest fixture that clears the in-memory module dicts in routes.py (_hub_cache, _batch_store, _stats_cache) and the rate-limiter window store. Cheap (microseconds per test) and eliminates a known class of order-dependent failures. Issue #218 - this is the architectural prerequisite. The follow-on work (per-test savepoint isolation via SQLAlchemy nested transactions, or DI-based replacement of routes.py module globals) can now be built on top because the connection lifecycle is finally well-defined. Local pytest delta: 65 baseline failures unchanged; this commit introduces zero new failures and unblocks future per-test isolation work.
Owner
Author
CI confirmation
Python: 15,009 passed / 70 failed / 1 error - identical to main. Zero regression, zero improvement, as predicted in the PR description. The architectural change is correct (StaticPool is the right pool class for in-memory SQLite) but it does not reduce the 65-test pollution baseline on its own. The remaining work in #218 needs to land alongside this for the count to drop. Merge options
Recommend option 1 - the file diff is tiny (97 lines), the change is obviously right, and follow-on PRs land cleaner once this foundation is in. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Architectural prerequisite for issue #218 (test isolation). Forces `poolclass=StaticPool` for in-memory SQLite URLs in the engine factory + adds a function-scoped autouse fixture that clears module-global dicts in routes.py and the rate-limiter window store between tests.
Why
The test suite uses `sqlite+aiosqlite://` (no path) for an in-memory database. Without StaticPool, every `async_session()` call may open a fresh aiosqlite connection, and each fresh in-memory connection sees its OWN empty database. A row written by session A is invisible to session B, which surfaces in CI as intermittent 'no such table: runs' / 'no rows returned' failures - the symptom diagnosed at length in `.autoresearch/test-isolation-results.tsv`.
This also explains why previous cleanup-fixture attempts in conftest.py made things strictly worse (65 failures -> 400+ errors): the fixture's row-wipe ran on a different in-memory DB than the test code subsequently read.
What changes
src/sandcastle/models/db.py
tests/conftest.py
What this does NOT do
Does not close #218 by itself. Local pytest delta with this change:
Zero new failures, but the 65-test pollution baseline remains. This is the architectural prerequisite.
Follow-ups in #218
After those three land, the baseline collapses to zero.